package com.newhis.controller;

import com.alibaba.fastjson.JSONObject;
import com.newhis.entity.LoginInfo;
import com.newhis.entity.SpUser;
import com.newhis.service.MainService;
import com.newhis.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Controller层
 */
@Api(value = "消息", description = "消息操作 API", position = 100, protocols = "http")
@RestController
@RequestMapping(value = "/main")
public class MainController {

    private static Logger logger = LoggerFactory.getLogger(MainController.class);

    @Autowired
    MainService mainService;
    @Autowired
    TokenService tokenService;

    @ApiOperation(
            value = "简单例子",
//            notes = "完整的消息内容列表",
//            produces = "application/json, application/xml",
//            consumes = "application/json, application/xml",
            httpMethod = "POST"
    )
    @RequestMapping("/out")
    @ResponseBody
    public String out() {
        logger.info("out SUCCESS");
        return "SUCCESS";
    }

    @ApiOperation(
            value = "根据ID获取人员",
//            notes = "根据ID获取人员",
//            produces = "application/json, application/xml",
//            consumes = "application/json, application/xml",
            httpMethod = "GET"
    )
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public SpUser getUserById(@ApiParam(name = "用户ID") @PathVariable(value = "id") Integer id){
        try {
            SpUser user = mainService.getUserById(id);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @ApiOperation(
            value = "修改用户名字",
            httpMethod = "POST"
    )
    @RequestMapping(value = "/user/{id}/{name}", method = RequestMethod.POST)
    public boolean setUserNameById(@ApiParam(name = "用户ID") @PathVariable(value = "id") Integer id,
                                   @ApiParam(name = "新用户名") @PathVariable(value = "name") String name){
        return mainService.setUserName(id, name);
    }

    @ApiOperation(
            value = "普通登录",
            httpMethod = "POST"
    )
    @PostMapping("/login")
    public Object login(@RequestBody LoginInfo li){
        JSONObject jsonObject = new JSONObject();
        LoginInfo userForBase = new LoginInfo(); //userService.findByUsername(user);
        userForBase.setBranchId(1);
        userForBase.setEmpId(100001);
        userForBase.setPassword("123456");
        userForBase.setIp("127.0.0.1");
        userForBase.setAut("");
        if(userForBase == null){
            jsonObject.put("message","登录失败,用户不存在");
            return jsonObject;
        }else {
            if (!userForBase.getPassword().equals("123456")){ //user.getPassword()
                jsonObject.put("message","登录失败,密码错误");
                return jsonObject;
            }else {
                String token = tokenService.getToken(userForBase);
                jsonObject.put("token", token);
                jsonObject.put("user", userForBase);
                return jsonObject;
            }
        }
    }
}
